package com.test.BRBInference;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@WebServlet(name = "BRBServlet", value = "/BRBServlet")
public class BRBServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();
        ArrayList<Object[]> modelList = (ArrayList<Object[]>) session.getAttribute("modelList");
        ArrayList<Object[]> dataList = (ArrayList<Object[]>) session.getAttribute("dataList");
        ArrayList<Object> uscale_temp = (ArrayList<Object>) session.getAttribute("uscale");


        ArrayList<ArrayList<Double>> modelArray = new ArrayList<ArrayList<Double>>();

        for (Object[] rule : modelList) {
            ArrayList<Double> model = new ArrayList<Double>();
            for (Object value : rule) {
                double temp = Double.parseDouble(value.toString());
                //System.out.println(temp);
                model.add(temp);
            }
            model.remove(0);
            modelArray.add(model);
        }


        ArrayList<ArrayList<Double>> dataArray = new ArrayList<ArrayList<Double>>();

        for (Object[] attr : dataList) {
            ArrayList<Double> data = new ArrayList<Double>();
            for (Object value : attr) {
                double temp = Double.parseDouble(value.toString());
                //System.out.println(temp);
                data.add(temp);
            }
            data.remove(0);
            dataArray.add(data);
        }

        ArrayList<Double> uscale = new ArrayList<>();
            for (Object value : uscale_temp) {
                double temp = Double.parseDouble(value.toString());
                uscale.add(temp);
            }


        ArrayList<Double> Actual_Result = new ArrayList<Double>();
        for(ArrayList<Double> attr :dataArray) {
            Actual_Result.add(attr.get(attr.size()-1));
        }

        double contribution_per = 0.0;
        int contribution_num = 0;
        boolean choose = true;

        if (ServletFileUpload.isMultipartContent(request)) { //请求表单的格式
            FileItemFactory fileItemFactory = new DiskFileItemFactory();  //缓存区
            ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);


            try {
                List<FileItem> list = servletFileUpload.parseRequest(request);  //表单列表

                    /*for (FileItem fileItem : list) {
                    if (fileItem.isFormField()) { //普通表
                        uscale.add(Double.parseDouble(fileItem.getString()));
                    }
                }*/

                String contribution = list.get(0).getString();

                if(contribution.equals("percent")) {
                    contribution_per = Double.parseDouble(list.get(1).getString());
                } else {
                    contribution_num = Integer.parseInt(list.get(1).getString());
                    choose = false;
                }

            } catch (FileUploadException e) {
                e.printStackTrace();
            }
        }

        BRBInference BRB = new BRBInference(uscale, dataArray, modelArray);
        BRB.setChoose(choose);
        BRB.setContribution_per(contribution_per);
        BRB.setContribution_num(contribution_num);

        BRB.attr_identify();
        BRB.fuse();
        BRB.contr_w_beta();
        BRB.data_Handle();

        Actual_Result = BRB.round(Actual_Result);

        request.getSession().setAttribute("Inference_Result", BRB.getInference_Result());
        request.getSession().setAttribute("Actual_Result", Actual_Result);
        request.getSession().setAttribute("Contri_num", BRB.data_attr);
        request.getSession().setAttribute("Contri_indivi", BRB.percent_contri_indivi);
        request.getSession().setAttribute("Contri_sum", BRB.percent_contri_sum);

        response.sendRedirect("/Java_web/Step3.jsp");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
